package com.lcm.learn.lcmcode.lambda;

import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @description:
 * @author: lcm
 * @create: 2019-08-30 17:04
 **/

public class LambdaTest {

    @Data
    static class Student {
        String name;
        double score;

        public Student(String name, double score) {
            this.name = name;
            this.score = score;
        }
    }

    public static <E> List<E> filter(List<E> list, Predicate<E> pred) {
        List<E> retList = new ArrayList<>();
        for (E e : list) {
            if (pred.test(e)) {
                retList.add(e);
            }
        }
        return retList;
    }

    public static <T, R> List<R> map(List<T> list, Function<T, R> mapper) {
        List<R> retList = new ArrayList<>(list.size());
        for (T e : list) {
            retList.add(mapper.apply(e));
        }
        return retList;
    }

    public static void main(String[] args){
        List<Student> students = Arrays.asList(new Student[] {
                new Student("zhangsan", 89d),
                new Student("lisi", 89d),
                new Student("wangwu", 98d) });

        List<Student> students2 = filter(students,t -> t.getScore() > 90);

        List<String> names = map(students, t -> t.getName());
        List<String> names1 = map(students,Student::getName);
    }

}
